package com.atlantis.apps.socius.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.atlantis.apps.socius.R;
import com.atlantis.apps.socius.util.SNConstants;

public class SNSettingsActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);

		SharedPreferences mySharedPreferences = getSharedPreferences(SNConstants.SNSETTINGS, Activity.MODE_PRIVATE);

		if (mySharedPreferences != null) {
			EditText emailTextBox = (EditText) findViewById(R.id.emailBox);
			emailTextBox.setText(mySharedPreferences.getString(SNConstants.FBACCOUNT, ""));

			Log.d(getClass().getSimpleName(), "Retrieved saved settings");
		}

		// Set the button functions
		Button button = (Button) findViewById(R.id.settingsBtn);
		button.setOnClickListener(submitSettings);
	}

	private OnClickListener submitSettings = new OnClickListener() {
		public void onClick(View v) {
			SharedPreferences mySharedPreferences = getSharedPreferences(SNConstants.SNSETTINGS, Activity.MODE_PRIVATE);

			// retrieve an editor to modify the shared preferences
			SharedPreferences.Editor editor = mySharedPreferences.edit();

			/*
			 * now store your primitive type values. In this case it is true, 1f
			 * and Hello! World
			 */
			editor.putString(SNConstants.FBACCOUNT, ((EditText) findViewById(R.id.emailBox)).getText().toString());
			// save the changes that you made
			editor.commit();
			Log.d(getClass().getSimpleName(), "Saved settings");

			Toast.makeText(v.getContext(), R.string.settingssaved, Toast.LENGTH_SHORT).show();
		}
	};
}